/**
 * 2010(c) Copyright Oceansoft Information System Co.,LTD. All rights reserved.
 * <p>
 * Compile: JDK1.6+
 * <p>
 * 版权所有(C)：江苏欧索软件有限公司
 * <p>
 * 公司名称：江苏欧索软件有限公司
 * <p>
 * 公司地址：苏州新区青山路1号索迪实训基地
 * <p>
 * 网址: http://www.oceansoft.com.cn
 * <p>
 * 版本: 权力阳光数据管理平台_1.0
 * <p>
 * 作者: 110510(朱伟家)
 * <p>
 * <p>
 * 文件名: DOCExporterImpl.java
 * <p>
 * 类产生时间:2013-3-24 下午1:39:51
 * <p>
 * 负责人: 110510(朱伟家)
 * <p>
 * 所在组: 权力阳光数据管理平台
 * <p>
 * 所在部门: 开发一部-技术二部
 * <p>
 * email：zhuwj@oceansoft.com.cn
 * <p>
 * <p>
 */

package com.oceansoft.core.common.util.impexp4doc;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import com.heavenlake.wordapi.Document;
import com.oceansoft.core.common.util.CommonUtilCache;
import com.oceansoft.core.common.util.impexp4doc.helper.BuildDOCHelper;
import com.oceansoft.core.common.util.impexp4doc.helper.ConfigFileLoadHelper;

public class DOCExporterImpl implements DOCExporter {

	// 全局配置
	HashMap<String, Object> configure = new HashMap<String, Object>();
	Document doc;
	String docXML;
	
	@SuppressWarnings("rawtypes")
	@Override
	public void loadConfig(String configName, Map extraCfg) {
		String configPath = ConfigFileLoadHelper.getConfigPath(configName);
		ConfigFileLoadHelper.loadCofingFile(configPath, configure, extraCfg);
	}

	@Override
	public void setConfig(Map<String, Object> configMap) {
		this.configure = (HashMap<String, Object>) configMap;
	}
	
	@Override
	public String exportWord(  String configfile, List<?> record, Map customMap) {
		loadConfig(configfile, null);
		//保存到服务器
		String tmpPath = CommonUtilCache
				.getValue(CommonUtilCache.KEY_WebRealPath)
				+ "/WEB-INF/classes/" + "sysconfig/impexp/tmp/"+System.currentTimeMillis()+".doc";
		try {
			 buildWord(record, customMap);
			
		/*	File f = new File(tmpPath);
			 FileWriter fw = new FileWriter(f);
			 fw.write(docXML);
			 fw.flush();
			 fw.close();*/
			 
			 
			
			 ByteArrayInputStream bais = new ByteArrayInputStream(docXML.getBytes("UTF-8"));
			POIFSFileSystem fs = new POIFSFileSystem();
			DirectoryEntry directory = fs.getRoot();
			
			DocumentEntry de = directory.createDocument("WordDocument", bais);

//				//文件输出流
			OutputStream os = new FileOutputStream(new File(tmpPath));
			
			fs.writeFilesystem(os);
			bais.close();
			os.flush();
			os.close();
			//doc.saveAs(tmpPath);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(doc!=null)
        	{
	        	try {
					doc.close(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
	        	doc = null;
        	}
		}
		return tmpPath;
	}
	
	
	@Override
	public void exportWord(OutputStream out, String configfile, List<?> record, Map customMap) {
		String tmpPath = exportWord(configfile,record,customMap);
		try
		{
		//转成输出流给客户下载
		FileInputStream fin = new FileInputStream(new File(tmpPath));
		//FileOutputStream fout = new FileOutputStream(new File(tmpPath));
       // ByteArrayOutputStream out = new ByteArrayOutputStream(1000);
        byte[] b = new byte[1000];
        int n;
        while ((n = fin.read(b)) != -1)
        {
            out.write(b, 0, n);
        }
        fin.close();
        // out.close();
        // return out.toByteArray();
		}catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	private void buildWord(List<?> record, Map customMap) throws Exception {
		//doc =BuildDOCHelper.buildDOC(configure, record, customMap);
	    docXML =BuildDOCHelper.buildDOC(configure, record, customMap);
	}

}
